/**
 * 获取字符的长度，full为true时，一个汉字算两个长度
 * @param {String} str 
 * @param {Boolean} full 
 */
function getTextLength(str, full) {
    let len = 0;
    for (let i = 0; i < str.length; i++) {
        let c = str.charCodeAt(i);
        //单字节加1 
                if (c >= 1 && c <= 126 || 65376 <= c && c <= 65439) {
            len++;
        } else {
            len += full ? 2 : 1;
        }
    }
    return len;
}

/**
 * rgba(255, 255, 255, 1) => #ffffff
 * @param {String} color 
 */ function transferColor(color = "") {
    let res = "#";
    color = color.replace(/^rgba?\(/, "").replace(/\)$/, "");
    color = color.split(", ");
    color.length > 3 ? color.length = 3 : "";
    for (let item of color) {
        item = parseInt(item || 0);
        if (item < 10) {
            res += "0" + item;
        } else {
            res += item.toString(16);
        }
    }
    return res;
}

function transferBorder(border = "") {
    let res = border.match(/(\w+)px\s(\w+)\s(.*)/);
    let obj = {};
    if (res) {
        obj = {
            width: +res[1],
            style: res[2],
            color: res[3]
        };
    }
    return res ? obj : null;
}

/**
 * 内边距，依次为上右下左
 * @param {*} padding 
 */ function transferPadding(padding = "0 0 0 0") {
    padding = padding.split(" ");
    for (let i = 0, len = padding.length; i < len; i++) {
        padding[i] = +padding[i].replace("px", "");
    }
    return padding;
}

/**
 * type1: 0, 25, 17, rgba(0, 0, 0, 0.3)
 * type2: rgba(0, 0, 0, 0.3) 0px 25px 17px 0px => (0, 25, 17, rgba(0, 0, 0, 0.3))
 * @param {*} shadow 
 */ function transferBoxShadow(shadow = "", type) {
    if (!shadow || shadow === "none") return;
    let color;
    let split;
    split = shadow.match(/(\w+)\s(\w+)\s(\w+)\s(rgb.*)/);
    if (split) {
        split.shift();
        shadow = split;
        color = split[3] || "#ffffff";
    } else {
        split = shadow.split(") ");
        color = split[0] + ")";
        shadow = split[1].split("px ");
    }
    return {
        offsetX: +shadow[0] || 0,
        offsetY: +shadow[1] || 0,
        blur: +shadow[2] || 0,
        color: color
    };
}

function getUid(prefix) {
    prefix = prefix || "";
    return prefix + "xxyxxyxx".replace(/[xy]/g, c => {
        let r = Math.random() * 16 | 0;
        let v = c === "x" ? r : r & 3 | 8;
        return v.toString(16);
    });
}

function showLoading(message) {
    if (wx.showLoading) {
        // 基础库 1.1.0 微信6.5.6版本开始支持，低版本需做兼容处理
        wx.showLoading({
            title: message,
            mask: true
        });
    } else {
        // 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
        wx.showToast({
            title: message,
            icon: "loading",
            mask: true,
            duration: 2e4
        });
    }
}

function hideLoading() {
    if (wx.hideLoading) {
        // 基础库 1.1.0 微信6.5.6版本开始支持，低版本需做兼容处理
        wx.hideLoading();
    } else {
        wx.hideToast();
    }
}

module.exports = {
    getTextLength: getTextLength,
    transferBorder: transferBorder,
    transferColor: transferColor,
    transferPadding: transferPadding,
    transferBoxShadow: transferBoxShadow,
    getUid: getUid,
    showLoading: showLoading,
    hideLoading: hideLoading
};